package com.wentao.thisService.service.impl;


import com.wentao.thisFacade.domain.result.LocationProvinceResult;
import com.wentao.thisService.service.CacheLocationService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Auther: ZhaiYanTao
 * @Date: 2020/3/27 16:48
 * @Description: 缓存service实现
 */
@Service
@CacheConfig(cacheNames = "locationMap")
public class CacheLocationServiceImpl implements CacheLocationService {

    private Map<String, LocationProvinceResult> locationMap = new ConcurrentHashMap<>();

    @Override
    @Cacheable
    public Map<String, LocationProvinceResult> getAllLocationProvinceCache() {
        return locationMap;
    }

    @Override
    @CachePut(key = "{#locationProvinceResult.provinceCode}")
    public Map<String, LocationProvinceResult> putToLocationProvinceCache(LocationProvinceResult locationProvinceResult) {
        locationMap.put(locationProvinceResult.getProvinceCode(), locationProvinceResult);
        return locationMap;
    }

    @Override
    @CachePut(key = "{#locationProvinceResult.provinceCode}")
    public Map<String, LocationProvinceResult> deleteLocationProvinceCache(LocationProvinceResult locationProvinceResult) {
        locationMap.remove(locationProvinceResult.getProvinceCode());
        return locationMap;
    }

    @Override
    @CachePut(key = "{#userInfo.userName}")
    public Map<String, LocationProvinceResult> clearLocationProvinceCache() {
        locationMap.clear();
        return locationMap;
    }
}
